package com.wryyyyy.controller;

import cn.hutool.core.map.MapUtil;
import com.wryyyyy.dto.AddLabelDTO;
import com.wryyyyy.dto.AddMaterialDTO;
import com.wryyyyy.dto.Result;
import com.wryyyyy.entity.Dish;
import com.wryyyyy.service.IDishLabelService;
import com.wryyyyy.service.IDishMaterialService;
import com.wryyyyy.service.IDishService;
import com.wryyyyy.service.IMaterialService;
import com.wryyyyy.utils.ImageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wryyyyy
 * @since 2023-11-12
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Resource
    private IDishService dishService;

    @Resource
    private IDishLabelService dishLabelService;

    @Resource
    private IDishMaterialService dishMaterialService;

    @Resource
    private IMaterialService materialService;

    /**
     * 查询菜品详情
     * @param id 菜品id
     * @param //number 标签数，在分页查询中只展示5条标签，在查询单个时展示所有标签
     * @return 菜品详情
     */
    @GetMapping("/{id}")
    public Result queryDishById(@PathVariable("id") String id) {
        return dishService.queryById(id);
    }

    /**
     * 新增菜品信息
     * @param params 菜品数据, 菜品用到的食材，菜品标签
     * @return 菜品id
     */
    @PostMapping("/save")
    @Transactional
    public Result save(@RequestBody Map<String, Object> params) {
        String id = MapUtil.get(params, "id", String.class);
        Dish dish = dishService.getById(id);

        String dishName = MapUtil.get(params, "dishName", String.class);
//        Double price = MapUtil.get(params, "price", Double.class);
        String introduce = MapUtil.get(params, "introduce", String.class);
        AddMaterialDTO[] materials = MapUtil.get(params, "materials", AddMaterialDTO[].class);
        AddLabelDTO[] labels = MapUtil.get(params, "labels", AddLabelDTO[].class);
        String[] urls = MapUtil.get(params, "urls", String[].class);

        if (dish == null) {
            dish = new Dish();
            dishService.save(dish);
        }
        dish.setDishName(dishName);
        dish.setIntroduce(introduce);
        dish.setImages(ImageUtil.setImages(urls));
        Double price = materialService.calculatePrice(Arrays.asList(materials));
        dish.setPrice(price);

        // 2.dish写入数据库
        dishService.update(dish);
        dishLabelService.addDishLabel(dish.getId(), Arrays.asList(labels));
        dishMaterialService.addDishMaterial(dish.getId(), Arrays.asList(materials));
        // 返回菜品id
        return Result.ok(dish.getId());
    }

    /**
     * 删除单个菜品
     * @param id 菜品id
     * @return 操作结果
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id){
        return dishService.deleteById(id);
    }

    /**
     * 批量删除菜品
     * @param ids id
     * @return 操作结果
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody String[] ids) {
        for (String id : ids) {
            dishService.deleteById(id);
        }
        return Result.ok();
    }

    /**
     * 根据 菜品名称关键字/菜品标签 分页查询菜品信息
     * @param name 菜品名称/标签关键字
     * @param current 页码
     * @return 菜品列表
     */
    @GetMapping("/list")
    public Result query(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "number", defaultValue = "5") Integer number
    ) {
        return dishService.queryByCondition(name, current, size, number);
    }
}
